﻿// C:\Users\Jorge\Documents\Visual Studio 2010\Projects\C#
//   \TestSignalGenLib\Program.cs
//
// Program to test the library SignalGenLib
//
// Programmer:  Jorge L. Orejel
//
// Last update: 02/24/2022 

using System;
using System.Collections.Generic;
using System.IO;

using SignalGenLib;

namespace TestSignalGenLib
{
   static class Program
   {
      private static FileStream fs;
      private static StreamWriter sw;
      public static GeneratingFn genFn;

      static void Main( string[] args )
      {
         fs = new FileStream( @"..\..\_TXT\out.txt", FileMode.Create );
         sw = new StreamWriter( fs );

         // genFn = new GeneratingFn( 5.0, 100.0 );       // Case A1
         // genFn = new GeneratingFn( 5.0, 500.0 );       // Case A2
         genFn = new GeneratingFn( 5.0, 1000.0 );       // Case A3


         // genFn = new GeneratingFn( 6.0, 100.0, 2.5 );  // Case F

         // genFn = new GeneratingFn( 7.0, 600.0, -3.0 ); // Case G

         IEnumerator<double> Sine = genFn.NextSineSignalValue();
         IEnumerator<double> Cosine = genFn.NextCosineSignalValue();
         IEnumerator<double> Square = genFn.NextSquareSignalValue();
         IEnumerator<double> Sawtooth = genFn.NextSawtoothSignalValue();
         IEnumerator<double> Triangle = genFn.NextTriangleSignalValue();
         IEnumerator<double> WhiteNoise = genFn.NextWhiteNoiseSignalValue();

         int n = 512;
         string signalName;

         signalName = "Sine";
         EnumerateValues( signalName, Sine, genFn.sineSignalValues, n );
         DisplayTimeStepAndZeroCrossings( genFn, signalName );
         genFn.PlotSineSignal();

         signalName = "Cosine";
         EnumerateValues( signalName, Cosine, genFn.cosineSignalValues, n );
         DisplayTimeStepAndZeroCrossings( genFn, signalName );
         genFn.PlotCosineSignal();

         signalName = "Square";
         EnumerateValues( signalName, Square, genFn.squareSignalValues, n );
         DisplayTimeStepAndZeroCrossings( genFn, signalName );
         genFn.PlotSquareSignal();

         signalName = "Sawtooth";
         EnumerateValues( signalName, Sawtooth, genFn.sawtoothSignalValues, n );
         DisplayTimeStepAndZeroCrossings( genFn, signalName );
         genFn.PlotSawtoothSignal();

         signalName = "Triangle";
         EnumerateValues( signalName, Triangle, genFn.triangleSignalValues, n );
         DisplayTimeStepAndZeroCrossings( genFn, signalName );
         genFn.PlotTriangleSignal();

         signalName = "WhiteNoise";
         EnumerateValues( signalName, WhiteNoise, genFn.whiteNoiseValues, n );
         DisplayTimeStepAndZeroCrossings( genFn, signalName );
         genFn.PlotWhiteNoiseSignal();

         sw.WriteLine();
         sw.Close();
         fs.Close();
         
         Console.WriteLine();
      }// Main

      private static void EnumerateValues( string signalName, IEnumerator<double> enumerator, 
                                           List<TimeMagnitude> list, int n )
      {
         string header = String.Format( "\n{0} signal values\n", signalName );

         Console.WriteLine( header ); sw.WriteLine( header );

         int j = 0;
         for ( int i = 0; i < n; ++i )
         {
            enumerator.MoveNext();

            double val = enumerator.Current;

            Display( String.Format( "{0,3}  {1,6:0.0000} {2,8:0.0000}   ", 
                                    i, list[ i ].Time, val ) );
            ++j;
            if ( ( j % 4 ) == 0 )
            {
               j = 0; NewLine();
            }
         }
      }// EnumerateValues

      private static void Display( string str )
      {
         Console.Write( str ); sw.Write( str );
      }// Display

      private static void NewLine()
      {
         Display( "\n" );
      }// NewLine

      private static void DisplayTimeStepAndZeroCrossings( GeneratingFn genFn, string signalName )
      {
        Display( String.Format( "\nTime step set by GeneratingFn constructor: {0,6:0.00000}", 
                                genFn.parameters.timeStep ) );

         if ( genFn.zeroCrossings == null || genFn.zeroCrossings.Count == 0 )
         {
            Display( "\n\nNo zero crossings." );
         }
         else
         {
            Display( String.Format( "\n\nZero crossings found by GeneratingFn.Next{0}SignalValue:\n", 
                                    signalName ) );

            int j = 0;
            for ( int i = 0; i < genFn.zeroCrossings.Count; ++i )
            {
               Display( String.Format( "{0,3}  {1,6:0.0000}  ", i, genFn.zeroCrossings[ i ] ) );
               ++j;
               if ( ( j % 7 ) == 0 )
               {
                  j = 0; NewLine(); ;
               }
            }
            NewLine();
         }
      }// DisplayTimeStepAndZeroCrossings
   }// Program (class)
}// TestSignalGenLib (namespace)
